package Servlet.Teacher;

import Entity.Grade.Grading;
import Entity.Grade.GroupGrade;
import Entity.MyTableData;
import Service.Teacher.GradeService;
import Service.Teacher.Imp.GradeServiceImp;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/25
 * Time: 15:57
 */

@WebServlet(urlPatterns = {"/getGrading", "/addGrading", "/getGroupGrade", "/addGroupGrade", "/getAllFinalGrade"},name = "GradeServlet")
public class GradeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getGrading".equals(url)) {
            getGrading(request, response);
        } else if ("/addGrading".equals(url)) {
            addGrading(request, response);
        } else if ("/getGroupGrade".equals(url)) {
            getGroupGrade(request, response);
        } else if ("/addGroupGrade".equals(url)) {
            addGroupGrade(request, response);
        } else if ("/getAllFinalGrade".equals(url)) {
            getAllFinalGrade(request, response);
        }

    }

    /**
     * 查询所有的最终成绩
     */
    private void getAllFinalGrade(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradeService gradeService = new GradeServiceImp();
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        MyTableData myTableData = gradeService.getAllFinalGrade(queryKey, offset, limit);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 答辩小组评分
     */
    private void addGroupGrade(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradeService gradeService = new GradeServiceImp();
        String gradeData = request.getParameter("gradeData");
        JSONObject jsonObject = JSONObject.parseObject(gradeData);
        String TNO = request.getParameter("userName");
        String sno = jsonObject.getString("sNO");
        String remark = jsonObject.getString("remark");
        Integer grade1 = jsonObject.getInteger("grade1");
        Integer grade2 = jsonObject.getInteger("grade2");
        Integer grade3 = jsonObject.getInteger("grade3");
        Integer grade4 = jsonObject.getInteger("grade4");
        String reply = jsonObject.getString("reply");
        GroupGrade groupGrade = new
                GroupGrade(null, sno, TNO, remark, grade1, grade2, grade3, grade4, reply, "审核中", null, null);
        Boolean flag = gradeService.addGroupGrade(groupGrade);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 查询指导小组的信息
     */
    private void getGroupGrade(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradeService gradeService = new GradeServiceImp();
        String NO = request.getParameter("userName");
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        int type = Integer.parseInt(request.getParameter("type"));
        MyTableData myTableData = gradeService.getGroupGrade(queryKey, offset, limit, NO, type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 指导老师添加一份定稿评分的数据
     */
    private void addGrading(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradeService gradeService = new GradeServiceImp();
        String gradeData = request.getParameter("gradeData");
        JSONObject jsonObject = JSONObject.parseObject(gradeData);
        String TNO = request.getParameter("userName");
        String sno = jsonObject.getString("sNO");
        String isOriginal = jsonObject.getString("original");
        String remark = jsonObject.getString("remark");
        Integer grade1 = jsonObject.getInteger("grade1");
        Integer grade2 = jsonObject.getInteger("grade2");
        Integer grade3 = jsonObject.getInteger("grade3");
        Grading grading = new
                Grading(null, sno, TNO, isOriginal, remark, grade1, grade2, grade3, "审核中", null, null);
        Boolean flag = gradeService.addGrading(grading);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 查询评阅老师的评分情况
     */
    private void getGrading(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradeService gradeService = new GradeServiceImp();
        String NO = request.getParameter("userName");
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        int type = Integer.parseInt(request.getParameter("type"));
        MyTableData myTableData = gradeService.getGradingByNO(queryKey, offset, limit, NO, type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


}
